package dialog;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lanhai.javaBean.JobBean;
import com.lanhai.model.Job;
import com.lanhai.utils.DialogUtils;
import com.lanhai.utils.HttpUtils;

import application.MainAction;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
 * 系统参数设置
 * @author liangmin
 *
 */
public class SettingPanel {

	//private static final Logger logger = Logger.getLogger(SettingPanel.class);
	@FXML
	private Button remove_job;
	@FXML
	private TextField chargerTime;
	@FXML
	private ListView<Job> dataList;
	@FXML
	private Button refresh;
	@FXML
	private ComboBox<String> objectList;
	private MainAction mainAction;
	
	public void init(MainAction mainAction) {
		this.mainAction = mainAction;
		refreshData();
		
		remove_job.setOnAction((ActionEvent t) -> {
			Job job = dataList.getSelectionModel().selectedItemProperty().getValue();
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("obj", "job");
			params.put("line", job.getLine());
			params.put("step", job.getStep());
			String url = "http://" + mainAction.main.serverUrl + "/del";
			JSONObject result = HttpUtils.httpPost2Json(url, params);
			boolean success = result.getBoolean("success");
			if(success){
				refreshData();
				DialogUtils.show("移除任务成功！");
			}else{
				DialogUtils.show("操作错误！");
			}
			
		});
		
		refresh.setOnAction((ActionEvent t) -> {
			refreshData();
		});
		
	}
	
	private void refreshData(){
		ObservableList<Job> dlist = FXCollections.observableArrayList(); 
		String url = "http://" + mainAction.main.serverUrl + "/queryData?job";
		JSONObject jo = HttpUtils.httpGet2Json(url);
		if(jo.getBoolean("success")){//返回成功
			JSONArray list = jo.getJSONArray("data");
			if(list.size() > 0){
				List<Job> jobList = JobBean.convertJOSNArray(list);
				for(Job j:jobList){
					dlist.add(j);
				}
			}
		}
		dataList.setItems(dlist);
	}
	
}
